package org.hazelnuts.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;


@Configuration
public class GlobalConfiguration {

    /**
     * destroy-method="close"的作用是当数据库连接不使用的时候,就把该连接重新放到数据池中,方便下次使用调用.
     */
    @Bean(destroyMethod = "close", name = "mysqlJdbcDataSource")
    @ConfigurationProperties("spring.datasource.hikari")
    public DataSource getMysqlJdbcDataSource() {
        return DataSourceBuilder.create().build();
    }
//
//    @Bean(destroyMethod = "close", name = "mysqlJdbcDataSource")
//    @ConfigurationProperties("spring.datasource.tomcat")
//    public DataSource getMysqlJdbcDataSource() {
//        return DataSourceBuilder.create().type(org.apache.tomcat.jdbc.pool.DataSource.class).build();
//    }
//
//    @Bean(destroyMethod = "close", name = "mysqlJdbcDataSource")
//    @ConfigurationProperties("spring.datasource.dbcp2")
//    public DataSource getMysqlJdbcDataSource() {
//        return DataSourceBuilder.create().type(org.apache.commons.dbcp2.BasicDataSource.class).build();
//    }
//
//    @Bean(destroyMethod = "close", name = "mysqlJdbcDataSource")
//    @ConfigurationProperties("spring.datasource.c3p0")
//    public DataSource getMysqlJdbcDataSource() {
//        return DataSourceBuilder.create().type(com.mchange.v2.c3p0.ComboPooledDataSource.class).build();
//    }
//
//    @Bean(destroyMethod = "close", name = "mysqlJdbcDataSource")
//    @ConfigurationProperties("spring.datasource.druid")
//    public DataSource getMysqlJdbcDataSource() {
//        return DataSourceBuilder.create().type(com.alibaba.druid.pool.DruidDataSource.class).build();
//    }

    @Bean(name = "mysqlJdbcTemplate")
    public JdbcTemplate getMysqlJdbcTemplate(@Autowired DataSource mysqlJdbcDataSource) {
        return new JdbcTemplate(mysqlJdbcDataSource);
    }

}
